//
//  HYTextView.m
//  HaiYue
//
//  Created by skyline on 2018/1/22.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYTextView.h"
@interface HYTextView ()<UITextViewDelegate>
@property (nonatomic, strong) UILabel *label;
@end
@implementation HYTextView
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.bounces = YES;
        self.layer.cornerRadius = GAP;
        self.contentInset = UIEdgeInsetsMake(0, GAP, -GAP, 0);
        self.font = [UIFont systemFontOfSize:15];
        self.delegate = self;
    }
    return self;
}

- (UILabel *)label
{
    if (!_label)
    {
        _label = [[UILabel alloc] init];
        _label.numberOfLines = 0;
        _label.textColor = TEXT_GRAY_COLOR;
        _label.font = [UIFont systemFontOfSize:15];
        [self addSubview:_label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(0);
            make.top.equalTo(self).with.offset(GAP);
        }];
    }
    return _label;
}

- (void)setPlacholder:(NSString *)placholder
{
    if (_placholder != placholder)
    {
        _placholder = placholder;
        self.label.text = _placholder;
        CGSize size = [self.label sizeThatFits:CGSizeMake(SCREEN_WIDTH - GAP * 4.0, MAXFLOAT)];
        [_label mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(size);
        }];
    }
}

- (void)setupBorderWhenBeginEditing
{
    self.layer.borderColor = THEME_COLOR.CGColor;
    self.layer.borderWidth = 0.5;
}

- (void)setupBorderWhenEndEditing
{
    self.layer.borderColor = [UIColor clearColor].CGColor;
    self.layer.borderWidth = 0.5;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [self setupBorderWhenBeginEditing];
    if (self.hydelegate && [self.hydelegate respondsToSelector:@selector(textViewShouldBeginEditing:)])
    {
        return [self.hydelegate textViewShouldBeginEditing:textView];
    }
    return YES;
}


- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [self setupBorderWhenEndEditing];
    if (self.hydelegate && [self.hydelegate respondsToSelector:@selector(textViewShouldEndEditing:)])
    {
        return [self.hydelegate textViewShouldEndEditing:textView];
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    if ([textView.text length] >0 )
    {
        _label.text = @"";
    }
    if (self.hydelegate && [self.hydelegate respondsToSelector:@selector(textViewDidChange:)])
    {
        [self.hydelegate textViewDidChange:textView];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text length] <= 0 || [textView.text isEqualToString:@""])
    {
        self.label.text = _placholder;
    }
    if (self.hydelegate && [self.hydelegate respondsToSelector:@selector(textViewDidEndEditing:)])
    {
        [self.hydelegate textViewDidEndEditing:textView];
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (self.hydelegate && [self.hydelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)])
    {
        return [self.hydelegate textView:textView shouldChangeTextInRange:range replacementText:text];
    }
    return YES;
}
@end
